package com.leaf.collegeidleapp;

import android.content.Context;
//import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.leaf.collegeidleapp.R;
import com.leaf.collegeidleapp.adapter.StarAdapter;
import com.leaf.collegeidleapp.bean.Collection;
import com.leaf.collegeidleapp.bean.Star;
import com.leaf.collegeidleapp.toucheventtest.SideslipListView;
import com.leaf.collegeidleapp.util.MyCollectionDbHelper;
import com.leaf.collegeidleapp.util.StarDBHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class StarActivity extends AppCompatActivity {


    public SideslipListView mSideslipListView;
    TextView tvStuId;
    List<Star> stars = new ArrayList<>();
    CustomAdapter adapter;
    StarDBHelper dbHelper;
    private static final String TAG = "StarActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_star);

        //返回
        ImageButton ibBack = findViewById(R.id.ib_back);
        ibBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


        tvStuId = findViewById(R.id.tv_stuId);
        tvStuId.setText(this.getIntent().getStringExtra("stu_number2"));
        mSideslipListView = (SideslipListView) findViewById(R.id.sideslipListView);
        dbHelper = new StarDBHelper(getApplicationContext(),StarDBHelper.DB_NAME,null,1);

        //BUG 不知道为什么不能按条件搜索，所以全部显示出来吧
        //应该是"stuId"没能生效
        stars = dbHelper.readAllStars();
        adapter = new CustomAdapter(getApplicationContext());
        adapter.setData(stars);
        mSideslipListView.setAdapter(adapter);//设置适配器


        //设置item点击事件
        mSideslipListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (mSideslipListView.isAllowItemClick()) {
                    Log.i(TAG, stars.get(position) + "被点击了");
                    Toast.makeText(StarActivity.this, stars.get(position) + "被点击了",
                            Toast.LENGTH_SHORT).show();
                }
            }
        });


    }


    /**
     * 自定义ListView适配器
     */

    class CustomAdapter extends BaseAdapter {
        private Context context;
        private LayoutInflater layoutInflater;
        private List<Star> stars = new ArrayList<>();
        HashMap<Integer, View> location = new HashMap<>();

        public CustomAdapter(Context context) {
            this.context = context;
            layoutInflater = LayoutInflater.from(context);
        }

        public void setData(List<Star> stars) {
            this.stars = stars;
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return stars.size();
        }

        @Override
        public Object getItem(int position) {
            return stars.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if (null == convertView) {
                convertView = View.inflate(StarActivity.this, R.layout.layout_star, null);
                Star star = (Star) getItem(position);
                viewHolder = new ViewHolder(convertView,star);
                //viewHolder.textView = (TextView) convertView.findViewById(R.id.textView);
                viewHolder.txtv_delete = (TextView) convertView.findViewById(R.id.txtv_delete);
                convertView.setTag(viewHolder);

            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            //viewHolder.textView.setText(mDataList.get(position));
            final int pos = position;
            viewHolder.txtv_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Star star = (Star)adapter.getItem(pos);

                    //由于"stuId"没生效，所以只能根据"phone"删除了
                    dbHelper.deleteMyStar(star.getPhone());
                    Toast.makeText(StarActivity.this, stars.get(pos) + "被删除了",
                            Toast.LENGTH_SHORT).show();
                    stars.remove(pos);
                    notifyDataSetChanged();
                    mSideslipListView.turnNormal();
                }
            });
            return convertView;
        }

        class ViewHolder {
            public TextView txtv_delete;
            ImageView ivStar;
            TextView tvName,tvPhone;


            public ViewHolder(View itemView,Star star) {
                tvName = itemView.findViewById(R.id.tv_name);
                tvPhone = itemView.findViewById(R.id.tv_phone);
                ivStar = itemView.findViewById(R.id.iv_star);
                tvName.setText(star.getStuId());
                tvPhone.setText(star.getPhone());
            }
        }
    }



}